package com.example.secondhandshop.model;

import android.util.Log;

import com.example.secondhandshop.bean.ApiAllGoodsData;
import com.example.secondhandshop.bean.Item;
import com.example.secondhandshop.bean.ResponseBody;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class AddOrderModel {

    private static final String BASE_URL = "https://api-store.openguet.cn/api/member/tran/trading";
    private static final String APP_ID = "2de64479320b423cbc05dd7e6c922999";
    private static final String APP_SECRET = "16786e5aa3e8a7f8e4f24940cb351fad802ca";
    private OkHttpClient client = new OkHttpClient();
    private final Gson gson = new Gson();
    private static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");

    public void postOrder(long buyerId, long goodsId, Integer price,long sellerId, OnProductsFetchedListener listener) {
        // 构建请求体
        Map<String, Object> bodyMap = new HashMap<>();
        bodyMap.put("buyerId", buyerId);
        bodyMap.put("goodsId", goodsId);
        bodyMap.put("price", price);
        bodyMap.put("sellerId", sellerId);

        String jsonBody = gson.toJson(bodyMap);
        Request request = new Request.Builder()
                .url(BASE_URL)
                .post(RequestBody.create(MEDIA_TYPE_JSON, jsonBody))
                .addHeader("appId", APP_ID)
                .addHeader("appSecret", APP_SECRET)
                .build();

        // 发起请求
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                listener.onFailure(e.getMessage()); // 捕获请求失败的情况
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String apiResponseBody = response.body().string();


                    // 使用 Gson 解析响应
                    ResponseBody<ApiAllGoodsData> responseBody = gson.fromJson(apiResponseBody, new TypeToken<ResponseBody<ApiAllGoodsData>>() {}.getType());

                    // 检查响应代码
                    if (responseBody.getCode() == 0) {
                        ApiAllGoodsData apiAllGoodsData = responseBody.getData(); // 获取 Data
                        List<Item> items = apiAllGoodsData != null ? apiAllGoodsData.getRecords() : null; // 安全获取记录，处理可能的 null
                        listener.onSuccess();
                    } else {
                        listener.onFailure(responseBody.getMsg()); // 使用实际消息
                    }
                } else {
                    listener.onFailure("请求失败: " + response.message()); // 捕获请求失败时的消息
                }
            }
        });
    }

    // 回调接口定义
    public interface OnProductsFetchedListener {
        void onSuccess();
        void onFailure(String error);
    }
}